import axios from 'axios'
import React, { useEffect, useState } from 'react'
import { Table, Button, Tag ,notification} from 'antd'
function AuditList(props) {
    const { username } = JSON.parse(localStorage.getItem("token"))
    const [dataList, setDataList] = useState([])
    const columns = [
        {
            title: '新闻标题',
            dataIndex: 'title',
            render: (title, item) => {
                return <a href={`#/news-manage/preview/${item.id}`}>{title}</a>
            }
        },
        {
            title: '作者',
            dataIndex: 'author',

        },
        {
            title: '新闻分类',
            dataIndex: 'categoryId',
        },
        {
            title: '审核状态',
            dataIndex: 'auditState',
            render: (auditState) => {
                const colorList = ['', 'orange', 'green', 'red']
                const AuditList = ['草稿箱', '审核中', '已通过', '未通过']
                return <Tag color={colorList[auditState]}>{AuditList[auditState]}</Tag>
            }
        },
        {
            title: '操作',
            dataIndex: '',
            key: 'key',
            render: (item) =>
                <div>

                    {
                        item.auditState == 1 && <Button onClick={()=>handleRervert(item)} danger>撤销</Button>
                    }

                    {
                        item.auditState == 2 && <Button onClick={()=>handlePublish(item)} >发布</Button>
                    }
                    {
                        item.auditState == 3 && <Button type='primary' onClick={()=>handleUpdate(item)}>更新</Button>
                    }
                </div>
        },
    ];
    // 撤销操作
    const handleRervert=(item)=>{
        setDataList(dataList.filter(data=>data.id!==item.id) )
        axios.patch("/news/"+item.id).then(res=>{
            const args = {
                message: '撤销成功',
                description: `你可以到草稿箱中查看你的新闻` ,
                duration: 3,
              };
              notification.open(args);
        })
    }
    //  更新
    const handleUpdate=(item)=>{
        props.history.push(`/news-manage/update/${item.id}`)
    } 
    // 发布
    const handlePublish=(item)=>{
        axios.patch("/news/"+item.id,{
            "publishState": 2,
            'publishTime':Date.now()
        }).then(res=>{
            const args = {
                message: 'Notification Title',
                description: `你可以到发布管理/已发布中查看你的新闻` ,
                duration: 3,
              };
              notification.open(args);
            props.history.push('/publish-manage/published')
        })
    }

    useEffect(() => {
        axios.get("/news?author=" + username + "&auditState_ne=0&publishState_lte=1&_expand=category").then(res => {
            console.log(res.data);
            setDataList(res.data)

        })
    }, [username])
    return (
        <div>
            <Table dataSource={dataList} columns={columns} pagination={{
                pageSize: 5,
            }} />;
        </div>
    )
}

export default AuditList